
///////////////////////////////////////////////////////////////////////////////
// Replication code for 'Ballots and burials: Electoral turnovers and the 
// health costs of elections during emergencies' 
// by Max Schaub, Héloïse Cloléry, Guillaume Kon Kam King, and Davide Morisi
// Forthcoming at the Journal of Politics
// July 2025
///////////////////////////////////////////////////////////////////////////////




//// Setup ////


// Install packages //

ssc install estout, replace // thanks to Ben Jann 
ssc install outreg, replace // thanks to John Luke Gallup
ssc install reghdfe, replace // thanks to Sergio Correia and Noah Constantine
ssc install did2s, replace // thanks to Kyle Butts
ssc install coefplot, replace // thanks to Ben Jann
ssc install psmatch2, replace // thanks to Edwin Leuven and Barbara Sianesi
ssc install psacalc, replace // thanks to Emily Oster


// Set working directory (replace ... with your directory) //

cd "..."


// Paths //

global Estimates "Estimates/"
global Figures "Figures/"
global Data "Data/"
global Code "Code/"

// Graphics setup //

graph set window fontface "Arial"
set scheme stcolor


// Program //

cap program drop rd_matrix
program define rd_matrix
mat list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
	estimates save "${Estimates}`name'", replace
}
eststo clear
matrix drop _all
end
 

// Control variables //

global controls log_MaleToFemaleRatio log_shareImmigrants log_shareBlueCollar log_PopDensity log_sq_PopDensity log65 log75 log80 log6575 log6580 log7580 log3 log_MedianStandardLiving10k log_basemort log_shareUnemp delta_Immigrants delta_Unemp delta_logStandardLiving log_nl2019 delta_log_nl



//// Tables ////


// Table 1, Table A4 //

use "${Data}municip_main.dta", clear

* Naive 
eststo main_naive: reg excessmort turnover
cap drop sample
gen sample = e(sample)
estadd local controls "Yes" , replace
estadd local fixed "No" , replace

* Controls only
eststo main_controls: reg excessmort turnover $controls i.ncandidates
estadd local controls "Yes" , replace
estadd local fixed "No" , replace

* Départements fixed effects 
eststo main_fe: areg excessmort turnover $controls i.ncandidates, absorb(dp)
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

* Matching 
set seed 12049
cap drop pscore weightmort
probit turnover $controls i.ncandidates, asis
predict pscore
set seed 12049
psmatch2 turnover, pscore(pscore) neighbor(1) noreplacement caliper(.025) 
rename _weight weightmort
eststo main_match: reg excessmort turnover $controls i.ncandidates if weightmort==1
cap drop samplematch 
gen samplematch = e(sample)
estadd local controls "Yes" , replace
estadd local fixed "No" , replace

* Interaction with share_left
cap drop turnover_share_left2014
gen turnover_share_left2014 = turnover*share_left2014
label variable turnover_share_left2014 "Turnover $\times$ vote share for left-leaning parties"
eststo main_left: areg excessmort turnover share_left2014 turnover_share_left2014 $controls i.ncandidates, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

* Stability in council membership
eststo main_council: areg excessmort share_stable $controls i.ncandidates, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

cap drop turnover_share_stable
gen turnover_share_stable = turnover*share_stable
label variable turnover_share_stable "Turnover  $\times$ share council remaining in place"
eststo main_council_interact: areg excessmort turnover share_stable turnover_share_stable $controls i.ncandidates, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

* Table 1
esttab main_naive main_controls main_fe main_match main_left main_council main_council_interact, keep(turnover share_left2014 turnover_share_left2014 share_stable turnover_share_stable) ///
	order(turnover turnover_share_left2014 share_stable turnover_share_stable share_left2014) ///
	varwidth(30) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N r2, fmt(%3s %3s %9.0gc %9.2f) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 
		
* Table A4
esttab main_naive main_controls main_fe main_match main_left main_council main_council_interact, drop(*ncandidates) ///
	order(turnover turnover_share_left2014 share_stable turnover_share_stable share_left2014) ///
	varwidth(30) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N r2, fmt(%3s %3s %9.0gc %9.2f) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 


// Table 2 //

use "${Data}municip_main.dta", clear

eststo clear
matrix drop _all

* RD estimates excess mortality, overall
rdrobust excessmort inc_win_margin2020, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort bias_mort robust_mort
rd_matrix

* RD Excess mortality, left-leaning
rdrobust excessmort inc_win_margin2020 if leftleaning==1, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort_left bias_mort_left robust_mort_left
rd_matrix

* RD estimates excess mortality, center-/right-leaning
rdrobust excessmort inc_win_margin2020 if leftleaning==0, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort_nonleft bias_mort_nonleft robust_mort_nonleft
rd_matrix

* adding information
eststo clear
estimates use "${Estimates}convent_mort"
eststo convent_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "20.17" , replace

estimates use "${Estimates}bias_mort"
eststo bias_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "31.14" , replace

estimates use "${Estimates}robust_mort"
eststo robust_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "31.14" , replace

estimates use "${Estimates}convent_mort_left"
eststo convent_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,453" , replace
estadd local bw "18.50" , replace

estimates use "${Estimates}bias_mort_left"
eststo bias_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,453" , replace
estadd local bw "27.63" , replace

estimates use "${Estimates}robust_mort_left"
eststo robust_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,453" , replace
estadd local bw "27.63" , replace

estimates use "${Estimates}convent_mort_nonleft"
eststo convent_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,459" , replace
estadd local bw "19.22" , replace

estimates use "${Estimates}bias_mort_nonleft"
eststo bias_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,459" , replace
estadd local bw "31.53" , replace

estimates use "${Estimates}robust_mort_nonleft"
eststo robust_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,459" , replace
estadd local bw "31.53" , replace

* Table 2
esttab convent_mort bias_mort robust_mort convent_mort_left bias_mort_left robust_mort_left convent_mort_nonleft bias_mort_nonleft robust_mort_nonleft,   ///	
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 
		


// Table 3 //

use "${Data}municip_main.dta", clear
		
eststo clear
matrix drop _all

* RD estimates center win
eststo Margin: rdrobust fcont2020 inc_win_margin2020, covs($controls cand* fcont2018) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent1 bias1 robust1
rd_matrix

* RD left-wing win
eststo Margin: rdrobust fperso2020 inc_win_margin2020, covs($controls cand* fperso2018) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent2 bias2 robust2
rd_matrix

* RD right-wing
eststo Margin: rdrobust fachat2020 inc_win_margin2020, covs($controls cand* fachat2018) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent3 bias3 robust3
rd_matrix

* Loop retrieving stored estimates
local estimate_names convent1 bias1 robust1 convent2 bias2 robust2 convent3 bias3 robust3

foreach name of local estimate_names {
    estimates use "${Estimates}`name'"
    eststo `name'
}

eststo clear
estimates use "${Estimates}convent1"
eststo convent1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "14.65" , replace

estimates use "${Estimates}bias1"
eststo bias1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "22.86" , replace

estimates use "${Estimates}robust1"
eststo robust1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "22.86" , replace

estimates use "${Estimates}convent2"
eststo convent2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "20.11" , replace

estimates use "${Estimates}bias2"
eststo bias2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "32.36" , replace

estimates use "${Estimates}robust2"
eststo robust2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "32.36" , replace

estimates use "${Estimates}convent3"
eststo convent3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "12.50" , replace

estimates use "${Estimates}bias3"
eststo bias3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "21.99" , replace

estimates use "${Estimates}robust3"
eststo robust3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "4,912" , replace
estadd local bw "21.99" , replace

esttab convent1 bias1 robust1 convent2 bias2 robust2 convent3 bias3 robust3,   ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 
		

// Table 4 //

use "${Data}excess_mort_panel.dta", clear
quietly include "${Code}did_multiplegt_dyn.ado" 
* multiplegt version used for estimation 

global multiplegt_settings effects(3) placebo(3) graphoptions(title() legend(off) xtitle(Months relative to turnover, margin(medsmall) size(medium)) ytitle(Excess mortality (%), margin(medsmall) size(medium)) xscale(range(-3 (1) 3)) xlabel(-3 (1) 3, format(%3.0f)) yline(0) scheme(s1mono))

global multiplegt_controls controls(t_healthcare_qual t_health_exp_pc t_life_exp_2017 t_resp_disease_prev t_pop_density t_share_older t_vdem_libdem t_election)
 
eststo clear
eststo did_no: did_multiplegt_dyn_ p_avg_all_ages nentity nyear_month treated_turnover_t, $multiplegt_settings graph_off
estadd scalar ntotal =  e(N_avg_total_effect)
estadd local controls = "No"

eststo did_controls: did_multiplegt_dyn_ p_avg_all_ages nentity nyear_month treated_turnover_t,  $multiplegt_settings $multiplegt_controls graph_off
estadd scalar ntotal =  e(N_avg_total_effect)
estadd local controls = "Yes": did_controls

esttab did_* ,  ///
	varwidth(45) lab wrap star($\dagger$ 0.1 * 0.05 ** 0.01)  se compress nogaps b(%9.2f) sfmt(%9.2gc) ///
		stats(ntotal controls, fmt(%9.0gc) labels( "N total effect"  "Controls")) ///
		coeflabels(*_1 "xxx")


// Table A1 //

* typeset directly in Latex file


// Table A2 //
	
use "${Data}municip_main.dta", clear

eststo clear
eststo main_naive: reg excessmort turnover
cap drop sample
gen sample = e(sample)
	
global sumvars MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareImmigrants shareBlueCollar shareUnemp MedianStandardLiving10k PopDensity100 shareUnemp shareImmigrants log_nl2019 delta_log_nl delta_Immigrants delta_Unemp delta_MedianStandardLiving10k ncandidates basemort  excessmort MaskWearing

estpost summarize $sumvars if sample==1
esttab ., cells("mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(%9.0fc))")  nonumber varwidth(32) wrap replace label noobs 


// Table A3 //

use "${Data}municip_main.dta", clear

set seed 12049
cap drop pscore weightmort
probit turnover $controls i.ncandidates, asis
predict pscore
set seed 12049
psmatch2 turnover, pscore(pscore) neighbor(1) noreplacement caliper(.025) 
rename _weight weightmort
reg excessmort turnover $controls i.ncandidates if weightmort==1
cap drop samplematch 
gen samplematch = e(sample)

global balvars MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareImmigrants shareBlueCollar shareUnemp MedianStandardLiving10k PopDensity100 log_nl2019 delta_log_nl delta_Immigrants delta_Unemp delta_MedianStandardLiving10k ncandidates basemort excessmort   

mata: mata clear
local i = 1
foreach var in $balvars {
    reg `var' turnover if weightmort==1 & samplematch==1
    outreg, keep(turnover)  rtitle("`: var label `var''") stats(b) ///
        noautosumm store(row`i')  starlevels(5 1) starloc(1)
    outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
        store(diff) note("")
    local ++i
}
outreg, replay(diff) 
local count: word count $balvars
mat sumstat = J(`count',6,.)

local i = 1
foreach var in $balvars {
    quietly: summarize `var' if turnover==0 & weightmort==1 & samplematch==1
    mat sumstat[`i',1] = r(N)
    mat sumstat[`i',2] = r(mean)
    mat sumstat[`i',3] = r(sd)
    quietly: summarize `var' if turnover==1 & weightmort==1 & samplematch==1
    mat sumstat[`i',4] = r(N)
    mat sumstat[`i',5] = r(mean)
    mat sumstat[`i',6] = r(sd)
    local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(gc,fc,fc,gc,fc,fc) sdec(3)
outreg,  ///
    replay(sumstat) merge(diff)nocenter note("")  replace ///
    ctitles("", No turnover, "", "", Turnover, "", "", "" \ "", n, mean, sd, n, mean, sd, Diff) ///
    multicol(1,2,3;1,5,3)
	
	
// Table A4 //

* see code for Table 1 above
	

// Table A5 //

use "${Data}municip_main.dta", clear

eststo clear
matrix drop _all

eststo LeftMargin: rdrobust excessmort left_margin, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort bias_mort robust_mort
rd_matrix

eststo LeftMargin: rdrobust excessmort left_margin if incumbent_left==1, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort_left bias_mort_left robust_mort_left
rd_matrix

eststo LeftMargin: rdrobust excessmort left_margin if incumbent_left==0, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent_mort_nonleft bias_mort_nonleft robust_mort_nonleft
rd_matrix

eststo clear
estimates use "${Estimates}convent_mort"
eststo convent_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "830" , replace
estadd local bw "20.06" , replace

estimates use "${Estimates}bias_mort"
eststo bias_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "830" , replace
estadd local bw "33.70" , replace

estimates use "${Estimates}robust_mort"
eststo robust_mort
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "830" , replace
estadd local bw "33.70" , replace

estimates use "${Estimates}convent_mort_left"
eststo convent_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "394" , replace
estadd local bw "9.79" , replace

estimates use "${Estimates}bias_mort_left"
eststo bias_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "394" , replace
estadd local bw "14.99" , replace

estimates use "${Estimates}robust_mort_left"
eststo robust_mort_left
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "394" , replace
estadd local bw "14.99" , replace

estimates use "${Estimates}convent_mort_nonleft"
eststo convent_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "436" , replace
estadd local bw "11.03" , replace

estimates use "${Estimates}bias_mort_nonleft"
eststo bias_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "436" , replace
estadd local bw "14.32" , replace

estimates use "${Estimates}robust_mort_nonleft"
eststo robust_mort_nonleft
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "436" , replace
estadd local bw "14.32" , replace

esttab convent_mort bias_mort robust_mort convent_mort_left bias_mort_left robust_mort_left convent_mort_nonleft bias_mort_nonleft robust_mort_nonleft,   ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 
		

// Table A6 //

use "${Data}municip_main.dta", clear

eststo winning_center: areg winning_center turnover $controls i.ncandidates, absorb(dp)
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace 
eststo winning_right: areg winning_right turnover $controls i.ncandidates, absorb(dp)
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo winning_left: areg winning_left turnover $controls i.ncandidates, absorb(dp)
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

esttab winning_center winning_left winning_right, keep(turnover) ///
	varwidth(32) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N r2, fmt(%3s %3s %9.0gc %9.2f) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 

		
// Table A7 //

use "${Data}municip_main.dta", clear

eststo clear
matrix drop _all

rdrobust winning_center inc_win_margin2020, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent1 bias1 robust1
rd_matrix

rdrobust winning_left inc_win_margin2020, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent2 bias2 robust2
rd_matrix

rdrobust winning_right inc_win_margin2020, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent3 bias3 robust3
rd_matrix

eststo clear
estimates use "${Estimates}convent1"
eststo convent1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "11.70" , replace

estimates use "${Estimates}bias1"
eststo bias1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "18.70" , replace

estimates use "${Estimates}robust1"
eststo robust1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "18.70" , replace

estimates use "${Estimates}convent2"
eststo convent2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "13.02" , replace

estimates use "${Estimates}bias2"
eststo bias2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "19.57" , replace

estimates use "${Estimates}robust2"
eststo robust2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "19.57" , replace

estimates use "${Estimates}convent3"
eststo convent3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "10.21" , replace

estimates use "${Estimates}bias3"
eststo bias3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "16.71" , replace

estimates use "${Estimates}robust3"
eststo robust3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 1,494" , replace
estadd local bw "16.71" , replace

esttab convent1 bias1 robust1 convent2 bias2 robust2 convent3 bias3 robust3,   ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 

		
// Table A8 //

use "${Data}municip_main.dta", clear

eststo clear
matrix drop _all

rdrobust MaskWearing left_margin, covs($controls cand* ndp*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent1 bias1 robust1
rd_matrix

rdrobust MaskWearing left_margin if incumbent_left==1, covs($controls cand* ndp*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent2 bias2 robust2
rd_matrix

rdrobust MaskWearing left_margin if incumbent_left==0, covs($controls cand* ndp*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) C
mat rownames C = convent3 bias3 robust3
rd_matrix

eststo clear
estimates use "${Estimates}convent1"
eststo convent1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 830" , replace
estadd local bw "10.25" , replace

estimates use "${Estimates}bias1"
eststo bias1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 830" , replace
estadd local bw "16.19" , replace

estimates use "${Estimates}robust1"
eststo robust1
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 830" , replace
estadd local bw "16.19" , replace

estimates use "${Estimates}convent2"
eststo convent2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 394" , replace
estadd local bw "7.31" , replace

estimates use "${Estimates}bias2"
eststo bias2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 394" , replace
estadd local bw "11.27" , replace

estimates use "${Estimates}robust2"
eststo robust2
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 394" , replace
estadd local bw "11.27" , replace

estimates use "${Estimates}convent3"
eststo convent3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 436" , replace
estadd local bw "5.97" , replace

estimates use "${Estimates}bias3"
eststo bias3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 436" , replace
estadd local bw "9.32" , replace

estimates use "${Estimates}robust3"
eststo robust3
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N " 436" , replace
estadd local bw "9.32" , replace


esttab convent1 bias1 robust1 convent2 bias2 robust2 convent3 bias3 robust3,   ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 


// Table A9 //

use "${Data}municip_main.dta", clear

eststo clear
eststo ideol_excess_mort_all: areg excessmort c.turnover##ib2.affiliation_win $controls i.ncandidates, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo m1: margins, dydx(turnover) at(affiliation_win==1) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates, absorb(dp)  
eststo m2: margins, dydx(turnover) at(affiliation_win==2) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates, absorb(dp)  
eststo m3: margins, dydx(turnover) at(affiliation_win==3) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo ideol_excess_mort_left: areg excessmort c.turnover##ib2.affiliation_win $controls i.ncandidates if leftleaning==1, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo m4: margins, dydx(turnover) at(affiliation_win==1) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates if leftleaning==1, absorb(dp) 
eststo m5: margins, dydx(turnover) at(affiliation_win==2) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates if leftleaning==1, absorb(dp) 
eststo m6: margins, dydx(turnover) at(affiliation_win==3) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo ideol_excess_mort_leftinc: areg excessmort c.turnover##ib2.affiliation_win $controls i.ncandidates if incumbent_left==1, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo m7: margins, dydx(turnover) at(affiliation_win==1) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates if incumbent_left==1, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo m8: margins, dydx(turnover) at(affiliation_win==2) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
areg excessmort c.turnover##i.affiliation_win $controls i.ncandidates if incumbent_left==1, absorb(dp) 
eststo m9: margins, dydx(turnover) at(affiliation_win==3) post
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

esttab m1 m2 m3 m4 m5 m6 m7 m8 m9,  ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N, fmt(%3s %3s %9.0gc) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 


// Table A10 //

use "${Data}municip_main.dta", clear

esttab ideol_excess_mort_all ideol_excess_mort_left ideol_excess_mort_leftinc, keep(turnover *affiliation_win *affiliation_win#c.turnover) order(*affiliation_win#c.turnover *affiliation_win ) ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01)  ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N, fmt(%3s %3s %9.0gc) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 

		
// Table A11 //

use "${Data}municip_main.dta", clear

global controls_nobasemort log_MaleToFemaleRatio log_shareImmigrants log_shareBlueCollar log_PopDensity log_sq_PopDensity log65 log75 log80 log6575 log6580 log7580 log3 log_MedianStandardLiving10k log_shareUnemp delta_Immigrants delta_Unemp delta_logStandardLiving log_nl2019 delta_log_nl

eststo base_left: areg basemort100 share_left2014_100 $controls_nobasemort, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo base_center: areg basemort100 share_center2014_100 $controls_nobasemort, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo base_right: areg basemort100 share_right2014_100 $controls_nobasemort, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

eststo excess_left: areg excessmort share_left2014_100 $controls, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo excess_center: areg excessmort share_center2014_100 $controls, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace
eststo excess_right: areg excessmort share_right2014_100 $controls, absorb(dp) 
estadd local controls "Yes" , replace
estadd local fixed "Yes" , replace

esttab base_left base_center base_right excess_left excess_center excess_right, keep(share_left2014* share_center2014* share_right*  _cons) ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls fixed  N r2_a, fmt(%3s %3s %9.0gc %9.2f) label("Pretreatment controls" "Départements FEs" "N" "R2")) replace 
		

// Table A12 //

use "${Data}municip_main.dta", clear

eststo clear
estpost correlate share_left2014 shareImmigrants MedianStandardLiving10k shareUnemp shareAbove80, matrix listwise 
esttab, unstack not noobs compress b(%9.2f) star(\dagger 0.1 * 0.05 ** 0.01) label varwidth(45) nonumbers nogaps replace 

eststo clear
corr share_left2014 log_emergencyservices log_generalpractitioners log_nurses log_pharmacies 
estpost correlate share_left2014 log_emergencyservices log_generalpractitioners log_nurses log_pharmacies, matrix listwise 
esttab, unstack not noobs compress b(%9.2f) star(\dagger 0.1 * 0.05 ** 0.01) label varwidth(45) nonumbers nogaps replace 


// Table A13 //

use "${Data}arretes.dta", clear

global balvars MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareImmigrants shareBlueCollar shareUnemp MedianStandardLiving10k PopDensity100 ncandidates basemort   

mata: mata clear
local i = 1
foreach var in $balvars {
    reg `var' turnover 
    outreg, keep(turnover)  rtitle("`: var label `var''") stats(b) ///
        noautosumm store(row`i')  starlevels(5 1) starloc(1)
    outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
        store(diff) note("")
    local ++i
}
outreg, replay(diff)
local count: word count $balvars
mat sumstat = J(`count',6,.)

local i = 1
foreach var in $balvars {
    quietly: summarize `var' if turnover==0 
    mat sumstat[`i',1] = r(N)
    mat sumstat[`i',2] = r(mean)
    mat sumstat[`i',3] = r(sd)
    quietly: summarize `var' if turnover==1 
    mat sumstat[`i',4] = r(N)
    mat sumstat[`i',5] = r(mean)
    mat sumstat[`i',6] = r(sd)
    local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(gc,fc,fc,gc,fc,fc) sdec(3)
outreg, ///
    replay(sumstat) merge(diff)nocenter note("")  replace ///
    ctitles("", No turnover, "", "", Turnover, "", "", "" \ "", n, mean, sd, n, mean, sd, Diff) ///
    multicol(1,2,3;1,5,3) 

	
// Table A14 //

use "${Data}arretes.dta", clear

global balvars MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareImmigrants shareBlueCollar shareUnemp MedianStandardLiving10k PopDensity100 ncandidates basemort   

mata: mata clear
local i = 1
foreach var in $balvars {
    reg `var' turnover if narretes!=.
    outreg, keep(turnover)  rtitle("`: var label `var''") stats(b) ///
        noautosumm store(row`i')  starlevels(5 1) starloc(1)
    outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
        store(diff) note("")
    local ++i
}
outreg, replay(diff)
local count: word count $balvars
mat sumstat = J(`count',6,.)

local i = 1
foreach var in $balvars {
    quietly: summarize `var' if turnover==0 & narretes!=.
    mat sumstat[`i',1] = r(N)
    mat sumstat[`i',2] = r(mean)
    mat sumstat[`i',3] = r(sd)
    quietly: summarize `var' if turnover==1  & narretes!=.
    mat sumstat[`i',4] = r(N)
    mat sumstat[`i',5] = r(mean)
    mat sumstat[`i',6] = r(sd)
    local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(gc,fc,fc,gc,fc,fc) sdec(3)
outreg, ///
    replay(sumstat) merge(diff)nocenter note("")  replace ///
    ctitles("", No turnover, "", "", Turnover, "", "", "" \ "", n, mean, sd, n, mean, sd, Diff) ///
    multicol(1,2,3;1,5,3)
	

// Table A15, Table A16 //

use "${Data}arretes.dta", clear

poisson n_covid i.turnover
eststo poissonr
eststo poissonmargins: margins turnover, post 

nbreg n_covid i.turnover
eststo nbregr
eststo nbmargins: margins turnover, post 

esttab poissonr nbregr, star(\dagger 0.1 * 0.05 ** 0.01) order(1.turnover) drop(0.turnover) label varwidth(45) nonumbers nogaps 
esttab poissonmargins nbmargins, star(\dagger 0.1 * 0.05 ** 0.01) order(1.turnover) label varwidth(45) nonumbers nogaps 

	
// Table A17 //

use "${Data}municip_main.dta", clear

preserve
collapse (mean) excessmort turnover $controls ncandidates, by(dp)
eststo reg_dp_both: reg excessmort turnover $controls ncandidates
sum turnover, d
margins, at(turnover==(0 0.33)) post coefl
lincom _b[2._at] - _b[1bn._at] 
estadd scalar est = r(estimate): reg_dp_both
estadd scalar p_val = r(p): reg_dp_both
estadd local controls "Yes": reg_dp_both
restore

preserve
keep if leftleaning==1
collapse (mean) excessmort turnover $controls ncandidates, by(dp)
eststo reg_dp_left: reg excessmort turnover $controls ncandidates
margins, at(turnover==(0 0.33)) post coefl
lincom _b[2._at] - _b[1bn._at] 
estadd scalar est = r(estimate): reg_dp_left
estadd scalar p_val = r(p): reg_dp_left
estadd local controls "Yes": reg_dp_left
restore

preserve
keep if leftleaning==0
collapse (mean) excessmort turnover $controls ncandidates, by(dp)
eststo reg_dp_nonleft: reg excessmort turnover $controls ncandidates
margins, at(turnover==(0 0.33)) post coefl
lincom _b[2._at] - _b[1bn._at] 
estadd scalar est = r(estimate): reg_dp_nonleft
estadd scalar p_val = r(p): reg_dp_nonleft
estadd local controls "Yes": reg_dp_nonleft
restore

esttab reg_dp_both reg_dp_left reg_dp_nonleft, keep(turnover)  ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls  N r2 est p_val, fmt(%3s %9.0gc %9.3f %9.3f) label("Pretreatment controls" "N" "R2" "Difference margins" "P-value")) replace 


// Table A18 //

* see code for Figure A12 below


// Table A19 //

use "${Data}excess_mort_panel.dta", clear

tab entity if democracy==1 & any_turnover==1
tab entity if democracy==1 & any_turnover==0
tab entity if democracy==0 & any_turnover==1
tab entity if democracy==0 & any_turnover==0

	


//// Figures ////


// Figure 1 //

use "${Data}municip_main.dta", clear

areg excessmort turnover##leftleaning $controls i.ncandidates, absorb(dp) 
eststo int_nonleft: margins turnover, at(leftleaning==0) coefl post

areg excessmort turnover##leftleaning $controls i.ncandidates, absorb(dp) 
eststo int_left: margins turnover, at(leftleaning==1) coefl post
						
coefplot (int_nonleft, label(Center-/right-leaning)) (int_left, label(Left-leaning)) ///
, drop(_cons) xline(0) recast(bar) ciopts(recast(rcap))  format(%9.2f) ///
coeflabels(0.turnover = "No turnover" 1.turnover = "Turnover") ytitle("Excess mortality", size(medsmall)) ///
citop barwidt(0.3) scheme(burd11) vertical
graph display, ysize(1) xsize(1.1) scale(1) 
graph export "${Figures}fig1.eps", replace


// Figure 2 //

use "${Data}municip_main.dta", clear

* RD estimates with rug plot, cp. https://www.statalist.org/forums/forum/general-stata-discussion/general/1767620-rug-plot-for-rdplot
cap program drop rd_manual
program define rd_manual
global x inc_win_margin2020
global x_min = -21
global x_max = 21
cap drop rdplot_*
reg excessmort $controls cand*
cap drop resid_excessmort
predict resid_excessmort, resid // residualize because no covs allowed for plotting below
reg excessmort $controls cand*
cap drop comp_excessmort
predict comp_excessmort, xb 
cap drop resid_comp_excessmort
gen resid_comp_excessmort = resid_excessmort + comp_excessmort
global y resid_comp_excessmort
global c 0
global y_min = 0.8
global y_max = 1.4
end

* All
rd_manual
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) covs($controls cand*) graph_options(legend(off) name(rd_all_ref, replace) )
graph display, ysize(1) xsize(1) scale(1) // non-manual as reference
cap drop rdplot_*
rd_manual
levelsof $x if $x > $x_min & $x < $x_max, local(levels)
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95) nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max), xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) xscale(range(-20 (10) 20)) xlabel(-20 (10) 20, format(%3.0f) nogrid labsize(small)) ylab(, format(%2.1f) nogrid) /// 
ytitle("Excess mortality factor") xtitle("Winning margin") name(rd_mort, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig2a.eps", replace

* Left
levelsof $x if $x > $x_min & $x < $x_max & leftleaning==1, local(levels)
rd_manual
cap drop rdplot_*
rdplot $y $x if $x > $x_min & $x < $x_max & leftleaning==1, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95) nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max  & leftleaning==1, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max)  & leftleaning==1, xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) xscale(range(-20 (10) 20)) xlabel(-20 (10) 20, format(%3.0f) nogrid labsize(small)) ylab(, format(%2.1f) nogrid) /// 
ytitle("Excess mortality factor") xtitle("Winning margin") name(rd_mort_left, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig2b.eps", replace

* Centre-right
levelsof $x if $x > $x_min & $x < $x_max & leftleaning==0, local(levels)
rd_manual
cap drop rdplot_*
rdplot $y $x if $x > $x_min & $x < $x_max & leftleaning==0, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95) nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max  & leftleaning==0, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max)  & leftleaning==0, xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) xscale(range(-20 (10) 20)) xlabel(-20 (10) 20, format(%3.0f) nogrid labsize(small)) ylab(, format(%2.1f) nogrid) /// 
ytitle("Excess mortality factor") xtitle("Winning margin") name(rd_mort_right, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig2c.eps", replace


// Figure 3 //

use "${Data}municip_main.dta", clear

cap program drop rd_manual_masks
program define rd_manual_masks
global x inc_win_margin2020
global x_min = -13
global x_max = 13
cap drop rdplot_*
reg MaskWearing $controls cand* 
cap drop resid_MaskWearing
predict resid_MaskWearing, resid // residualize because no covs allowed for plotting below
reg MaskWearing $controls cand* if ExternalDecision==0
cap drop comp_MaskWearing
predict comp_MaskWearing, xb 
cap drop resid_comp_MaskWearing
gen resid_comp_MaskWearing = resid_MaskWearing + comp_MaskWearing
global y resid_MaskWearing
global c 0
global y_min= 0
global y_max = 0.15
end

rd_manual_masks
levelsof $x if $x > $x_min & $x < $x_max & ExternalDecision==0, local(levels)
rdplot MaskWearing $x if $x > $x_min & $x < $x_max & ExternalDecision==0, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95)  nbins(12 12)

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max), xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) xscale(range(-12 (3) 12)) xlabel(-12 (3) 12, format(%3.0f) nogrid labsize(small)) yscale(range($y_min $y_max)) ylab($y_min (0.05) $y_max, format(%3.2f) nogrid) /// 
ytitle("Excess mortality factor") xtitle("Winning margin") name(rd_masks, replace)
graph display, ysize(1) xsize(1.1) scale(1) 
graph export "${Figures}fig3.eps", replace

* Table
matrix drop _all
eststo clear
eststo Turnover: rdrobust MaskWearing inc_win_margin2020 if ExternalDecision==0, covs($controls cand*) all 
esttab, se nostar
mat list r(coefs) 
mat rename r(coefs) D
mat rownames D = convent_mask bias_mask robust_mask
mat list D
eststo clear
local rnames : rownames D
local models : coleq D
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = D[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = D[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
	estimates save "${Estimates}`name'", replace
}

eststo clear
estimates use "${Estimates}convent_mask"
eststo convent_mask
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,914" , replace
estadd local bw "11.17" , replace

estimates use "${Estimates}bias_mask"
eststo bias_mask
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,914" , replace
estadd local bw "21.27" , replace

estimates use "${Estimates}robust_mask"
eststo robust_mask
estadd local controls "Yes" , replace
estadd local cand "Yes" , replace
estadd local N "2,914" , replace
estadd local bw "21.27" , replace

esttab convent_mask bias_mask robust_mask, ///
	varwidth(45) lab wrap star(\dagger 0.1 * 0.05 ** 0.01) ///
		b(%9.3f) sfmt(%9.3gc) se compress nogaps mlabels(,none) stats(controls cand  N bw, fmt(%3s %3s %5s %5s) label("Pretreatment controls" "Candidate FEs"  "N" "Bandwidth")) replace 


// Figure 4 //

* see ballots&burials.R


// Figure 5 //

use "${Data}municip_main.dta", clear

* fcont
eststo fcont: rdrobust fcont2020 inc_win_margin2020, covs($controls cand* fcont2018) all 
global yr fcont2020
global x2 fcont2018
global x inc_win_margin2020
global x_min = -20
global x_max = 20
cap drop rdplot_*
reg $yr $controls cand* $x2 
cap drop resid_$yr
predict resid_$yr, resid // residualize because no covs allowed for plotting below
reg $yr $controls cand* $x2 
cap drop comp_$yr
predict comp_$yr, xb 
cap drop resid_comp_$yr
gen resid_comp_$yr = resid_$yr + comp_$yr
global y resid_comp_$yr
global c 0
global y_min = 0.8
global y_max = 1.4
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) )
graph display, ysize(1) xsize(1) scale(1) // non-manual as reference
cap drop rdplot_*
levelsof $x if $x > $x_min & $x < $x_max, local(levels)
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95) nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max), xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) yscale(range(0 (25) 75)) xscale(range($x_min (10) $x_max)) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(0 (25) 75, format(%2.0f) nogrid) /// 
ytitle("Compulsory contributions") xtitle("Winning margin") name($yr, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5a1.png", replace

* without rug, copy-pasted rug
rdplot $yr $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(0 (25) 75, format(%2.0f) nogrid) ytitle("Compulsory contributions") xtitle("Winning margin")) nbins(12 12) 
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5a.png", replace
* final figure combines fig5a1 and fig5a

* fperso
eststo fperso: rdrobust fperso2020 inc_win_margin2020, covs($controls cand* fperso2018) all 
global yr fperso2020
global x2 fperso2018 
global x inc_win_margin2020
global x_min = -20
global x_max = 20
cap drop rdplot_*
reg $yr $controls cand* $x2 if $x > $x_min & $x < $x_max
cap drop resid_$yr
predict resid_$yr, resid // residualize because no covs allowed for plotting below
reg $yr $controls cand* $x2 if $x > $x_min & $x < $x_max
cap drop comp_$yr
predict comp_$yr, xb 
cap drop resid_comp_$yr
gen resid_comp_$yr = resid_$yr + comp_$yr
global y resid_comp_$yr
global c 0
global y_min = 0.8
global y_max = 1.4
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) )
graph display, ysize(1) xsize(1) scale(1) // non-manual as reference
cap drop rdplot_*
levelsof $x if $x > $x_min & $x < $x_max, local(levels)
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95) nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max), xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) yscale(range(300 (100) 600)) xscale(range($x_min (10) $x_max)) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(300 (100) 600, format(%2.0f) nogrid) /// 
ytitle("Personnel expenses") xtitle("Winning margin") name($yr, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5b1.png", replace

* without rug, copy-paste rug
rdplot $yr $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) xscale(range($x_min (10) $x_max)) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(400 (100) 600, format(%2.0f) nogrid)  ytitle("Personnel expenses") xtitle("Winning margin")) nbins(12 12) 
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5b.png", replace

* fachat
eststo fachat: rdrobust fachat2020 inc_win_margin2020, covs($controls cand* fachat2018) all 
global yr fachat2020
global x2 fachat2018 
global x inc_win_margin2020
global x_min = -20
global x_max = 20
cap drop rdplot_*
reg $yr $controls cand* $x2 
cap drop resid_$yr
predict resid_$yr, resid // residualize because no covs allowed for plotting below
reg $yr $controls cand* $x2 
cap drop comp_$yr
predict comp_$yr, xb 
cap drop resid_comp_$yr
gen resid_comp_$yr = resid_$yr + comp_$yr
global y resid_comp_$yr
global c 0
global y_min = 0.8
global y_max = 1.4
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) )
graph display, ysize(1) xsize(1) scale(1) // non-manual as reference
cap drop rdplot_*
levelsof $x if $x > $x_min & $x < $x_max, local(levels)
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) graph_options(aspectratio(1) legend(off)) genvars hide ci(95)  nbins(12 12) 

twoway (scatter rdplot_mean_y rdplot_mean_bin if rdplot_mean_bin>$x_min & rdplot_mean_bin<$x_max, sort msize(small) mcolor(gs10) ) ///
(function `e(eq_l)' , range($x_min 0) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(function `e(eq_r)' , range(0 $x_max) lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(rspike $y $y $x if ($x > $x_min & $x < $x_max) & ($y > $y_min & $y < $y_max), xtick(`levels', tpos(inside) tlength(*2) tlc(gs8))), ///
legend(off) xline(0) yscale(range(150 (50) 300)) xscale(range($x_min (10) $x_max)) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(150 (50) 300, format(%2.0f) nogrid) /// 
ytitle("External purchases") xtitle("Winning margin") name($yr, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5c1.png", replace

* without rug, copy-paste rug
rdplot $y $x if $x > $x_min & $x < $x_max, all p(1) covs($controls  cand* $x2) graph_options(legend(off) name($y, replace) yscale(range(200 (50) 350)) xscale(range($x_min (10) $x_max)) xlabel($x_min (10) $x_max, format(%3.0f) nogrid labsize(small)) ylab(200 (50) 350, format(%2.0f) nogrid) ytitle("External purchases") xtitle("Winning margin")) nbins(12 12) 
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig5c.png", replace

	
// Figure 6 //

* for Figure 6a, see ballots&burials.R

* Figure 6b

use "${Data}excess_mort_panel.dta", clear

global multiplegt_settings effects(3) placebo(3) graphoptions(title() legend(off) xtitle(Months relative to turnover, margin(medsmall) size(medium)) ytitle(Excess mortality (%), margin(medsmall) size(medium)) xscale(range(-3 (1) 3)) xlabel(-3 (1) 3, format(%3.0f)) yline(0) scheme(s1mono))

did_multiplegt_dyn p_avg_all_ages nentity nyear_month treated_turnover_t, $multiplegt_settings 
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}fig6b.eps", replace

		
// Figure A1 //

* replicated from public source, see link 	

	
// Figure A2 //

* replicated from public source, see link 	


// Figure A3 //

* see ballots&burials.R


// Figure A4 //

* created in a graphics program, not part of replication package 


// Figure A5 //

use "${Data}municip_main.dta", clear

cap drop excessmort100
gen excessmort100 = excessmort*100 // for scaling with the underlying histogram
areg excessmort100 c.turnover##c.share_stable $controls , absorb(dp)
margins, dydx(turnover) at(share_stable==(.1 (.1) .5))
marginsplot, addplot(hist share_stable if turnover==1 & (share_stable>=0.055 & share_stable<.5), color(%30) bins(50)) recast(scatter) yline(0) yscale(range(-10 (5) 20))  ylabel(-10 "-0.10" 10 "0.10" 20 "0.20", format(%3.2f) nogrid) legend(off)  ///
	title("") ytitle("Effect of turnover on excess mortality", size(medlarge)) xtitle("Share of council members remaining in place", size(medlarge) margin(medium)) xsize(5.5) ysize(5)  xlabel(.2 (.1) .5, format(%3.2f) nogrid ) plotopts(msym(o)) scheme(burd10) ciopts(lwidth(medsmall) msize(small))
graph export "${Figures}figA5.pdf", replace
	

// Figure A6 //

use "${Data}municip_main.dta", clear

graph bar, over(affiliation_win) by(turnover_graph,  note("")) name(ideology_after_turnover, replace) 	ytitle("Percentage of winning candidates", size(medsmall)) xsize(5) blabel(total, format(%9.1f) size(vsmall)) scheme(burd11) ylab(, nogrid) subtitle(,nobox)
graph display, ysize(1) xsize(1.5) scale(1.5) 
graph export "${Figures}figA6.pdf", replace


// Figure A7 //

use "${Data}municip_main.dta", clear

* vulnerability 2020
reg vulnerability100 i.leftleaning
eststo vulnerability: margins leftleaning, post coefl
coefplot vulnerability, legend(off) vertical recast(bar) coeflabels(0.leftleaning = "Non-left-leaning" 1.leftleaning = "Left-leaning") barwidth(0.55) fcolor(*1.5) yscale(range(35 (5) 65)) ylab(35 (5) 65) ///
	ciopts(recast(rcap)) citop format(%9.0f) scheme(burd11)  ///
						ytitle("Social vulnerability index") xtitle("") name(vulnerability, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA7a.pdf", replace

* health services 2016
reg services100 i.leftleaning
eststo services: margins leftleaning, post coefl
coefplot services, legend(off) vertical recast(bar) coeflabels(0.leftleaning = "Non-left-leaning" 1.leftleaning = "Left-leaning") barwidth(0.55) fcolor(*1.5) yscale(range(0 (3) 21)) ylab(0 (3) 21)  ///
	ciopts(recast(rcap)) citop format(%9.0f) scheme(burd11)  ///
						ytitle("Social service provision index") xtitle("") name(services, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA7b.pdf", replace

* vulnerability 2014
sum vulnerability2014 
gen vulnerability2014100 = vulnerability2014+abs(r(min))
sum vulnerability2014100
replace vulnerability2014100 = (vulnerability2014100/r(max))* 100
label variable vulnerability2014100 "Vulnerability index, 2014"

reg vulnerability2014100 i.leftleaning
eststo vulnerability2014: margins leftleaning, post coefl
coefplot vulnerability2014, legend(off) vertical recast(bar) coeflabels(0.leftleaning = "Non-left-leaning" 1.leftleaning = "Left-leaning") barwidth(0.55) fcolor(*1.5) yscale(range(35 (5) 65)) ylab(35 (5) 65) ///
	ciopts(recast(rcap)) citop format(%9.0f) scheme(burd11)  ///
						ytitle("Social vulnerability index") xtitle("") name(vulnerability2014, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA7c.pdf", replace

* health services 2016
cap drop services2016100 services2016
alpha log_generalpractitioners2016 log_nurses2016 log_pharmacies2016 log_elderlyhome2016, gen(services2016) std
sum services2016 
gen services2016100 = services2016+abs(r(min))
sum services2016100
replace services2016100 = (services2016100/r(max))* 100
label variable services2016100 "Health service provision index, 2016"

reg services2016100 i.leftleaning
eststo services2016: margins leftleaning, post coefl
coefplot services2016, legend(off) vertical recast(bar) coeflabels(0.leftleaning = "Non-left-leaning" 1.leftleaning = "Left-leaning") barwidth(0.55) fcolor(*1.5) yscale(range(0 (3) 21)) ylab(0 (3) 21)  ///
	ciopts(recast(rcap)) citop format(%9.0f) scheme(burd11)  ///
						ytitle("Social service provision index") xtitle("") name(services2016, replace)
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA7d.pdf", replace

	
// Figure A8 //
		
* see ballots&burials.R


// Figure A9 //
		
* see ballots&burials.R


// Figure A10 //
		
* see ballots&burials.R


// Figure A11 //

use "${Data}municip_main.dta", clear

* test
rddensity inc_win_margin2020 
* residualized form, taking into account influence of controls
cap drop residual_inc_margin
areg inc_win_margin2020 $controls i.ncandidates, absorb(dp)
predict residual_inc_margin, resid
rddensity residual_inc_margin 

* density plot
cap drop left
cap drop right
gen left = residual_inc_margin if residual_inc_margin < 0 & residual_inc_margin>-100
gen right = residual_inc_margin if residual_inc_margin >= 0 & residual_inc_margin<100

twoway (histogram left, bin(30) color(blue%50)) ///
       (histogram right, bin(30) color(red%50)), ///
       legend(order(1 "Left of Threshold" 2 "Right of Threshold")) ///
       xline(0, lcolor(black)) title("") legend(position(5)) ytitle("Density winning/losing margin")
graph export "${Figures}figA11.pdf", replace


// Figure A12 and Table A18 //

use "${Data}municip_main.dta", clear

local checkvars MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareBlueCollar MedianStandardLiving10k log_PopDensity basemort shareUnemp shareImmigrants log_nl2019 delta_log_nl delta_Immigrants delta_Unemp delta_MedianStandardLiving10k // non-logged control variables, for easier interpretability
local num_controls : word count `checkvars' // matrix row length
matrix results = J(`num_controls', 3, .)  // rows for each control, 3 columns
local i = 1 // counter
foreach dv in `checkvars' { // loop
    local ivs ""
    foreach iv in `checkvars' {
        if "`iv'" != "`dv'" {
            local ivs `ivs' `iv'
        }
    }
    rdrobust `dv' inc_win_margin2020, covs(`ivs' cand*) all 
    matrix results[`i', 1] = e(tau_bc)     // robust coefficient
    matrix results[`i', 2] = e(se_tau_rb) // se
    * Calculate the p-value
    scalar t_stat = e(tau_bc) / e(se_tau_rb)
    scalar p_value = 2 * (1 - normal(abs(t_stat)))  
    matrix results[`i', 3] = p_value              
	local label : var label `dv'  
	rdplot `dv' inc_win_margin2020 if inc_win_margin2020>-75 & inc_win_margin2020<75, covs(`ivs' cand*) p(1) /// 
	all masspoints(off)  graph_options(legend(pos(6)) title("")  ///
	xscale(range(-75 (25) 75)) xlabel(-75 (25) 75, format(%3.0f) nogrid labsize(small)) ylab(, nogrid) leg(off) ///
	ytitle("`label'", ) xtitle("Winning margin", ) name(`dv', replace)) 
	graph display, ysize(1) xsize(1.1) scale(1) 
    local i = `i' + 1
}

* Figure A12
graph combine MaleToFemaleRatio shareAbove65 shareAbove75 shareAbove80 shareBlueCollar MedianStandardLiving10k log_PopDensity basemort shareUnemp shareImmigrants log_nl2019 delta_log_nl delta_Immigrants delta_Unemp delta_MedianStandardLiving10k, col(3) name(figA12, replace)
graph display, ysize(1.414) xsize(1) scale(1) 
graph export "${Figures}figA12.pdf", replace

* Table A18
matrix rownames results = `checkvars'
matrix colnames results = b_robust se_robust p_robust
matrix list results
estout matrix(results, fmt(3 3 2)) 

* Oster bounds
areg excessmort turnover $controls i.ncandidates, absorb(dp)
local rmax = e(r2)*1.3
psacalc delta turnover, rmax(`rmax') model(regress excessmort turnover $controls i.ncandidates i.dp)


// Figure A13 //

use "${Data}excess_mort_panel.dta", clear

twoway (lpoly p_avg_all_ages nyear_month if democracy == 1,) ///
       (lpoly p_avg_all_ages nyear_month if democracy == 0, lpattern(dash)), ///
       legend(order(1 "Democracies" 2 "Flawed democracies/autocracies") position(6)) ///
       title("") xtitle("") ytitle("Excess mortality (%)", margin(medsmall)) ///
       xlabel(1 "Jan 2020" 7 "Jul 2020" 13 "Jan 2021" 19 "Jul 2021" ///
              25 "Jan 2022" 31 "Jul 2022" 37 "Jan 2023" 43 "Jul 2023") scheme(s1mono)
graph display, ysize(1) xsize(2.2) scale(1.2) 
graph export "${Figures}figA13.pdf", replace


// Figure A14 //

use "${Data}excess_mort_panel.dta", clear

eststo clear
eststo did_dem: did_multiplegt_dyn p_avg_all_ages nentity nyear_month treated_turnover_t if democracy==1, $multiplegt_settings
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA14a.pdf", replace

eststo did_nodem: did_multiplegt_dyn p_avg_all_ages nentity nyear_month treated_turnover_t if democracy==0, $multiplegt_settings
graph display, ysize(1) xsize(1) scale(1) 
graph export "${Figures}figA14b.pdf", replace












